package com.bingxue.edu.parent.controller;

import com.bingxue.edu.common.enums.EnrollmentStatusEnum;
import com.bingxue.edu.common.enums.OperateSourceEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.management.education.model.query.EnrollClassQuery;
import com.bingxue.edu.management.education.model.req.ClassChangeReq;
import com.bingxue.edu.management.education.model.req.ClassChangeRevertReq;
import com.bingxue.edu.management.education.service.ClassChangeService;
import com.bingxue.edu.parent.model.query.ParentStudentClassQuery;
import com.bingxue.edu.parent.model.query.StudentClassChangeRecordQuery;
import com.bingxue.edu.parent.model.resp.StudentClassChangeRecordResp;
import com.bingxue.edu.parent.model.resp.StudentClassInfoResp;
import com.bingxue.edu.parent.model.resp.StudentClassLessonResp;
import com.bingxue.edu.parent.model.resp.StudentEnrollClassResp;
import com.bingxue.edu.parent.service.StudentClassService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author panbo
 * @since 2024/10/28
 */
@Tag(name = "家长端小程序-班级教务")
@RestController
@RequestMapping("/p/class")
@RequiredArgsConstructor
public class StudentClassController extends ParentBaseController {

    private final StudentClassService studentClassService;
    private final ClassChangeService classChangeService;

    @Operation(summary = "分页查询报名班级")
    @GetMapping("/getEnrollClassPage")
    public Response<Pagination<StudentEnrollClassResp>> getEnrollClassPage(@Validated EnrollClassQuery query, @Validated PageQuery pageQuery) {
        this.checkStudentRelation(query.getStudentId());
        query.setEnrollStatus(EnrollmentStatusEnum.NORMAL);
        Pagination<StudentEnrollClassResp> page = studentClassService.getEnrollClassPage(query, pageQuery);
        return Response.success(page);
    }

    @Operation(summary = "分页查询上课班级")
    @GetMapping("/getStudentClassList")
    public Response<List<StudentClassInfoResp>> getStudentClassList(@Validated ParentStudentClassQuery query) {
        this.checkStudentRelation(query.getStudentId());
        List<StudentClassInfoResp> list = studentClassService.getStudentClassList(query);
        return Response.success(list);
    }

    @Operation(summary = "查询班级课次列表")
    @GetMapping("/lesson/list/{classId}")
    public Response<List<StudentClassLessonResp>> getClassLessonList(@PathVariable String classId) {
        return Response.success(studentClassService.getClassLessonList(classId));
    }

    @Operation(summary = "转班调课")
    @PostMapping("/change/create")
    public Response<Void> changeClassOrLesson(@RequestBody ClassChangeReq req) {
        this.checkStudentRelation(req.getStudentId());
        req.setOperateSource(OperateSourceEnum.PARENT);
        studentClassService.changeClassOrLesson(req);
        return Response.success();
    }

    @Operation(summary = "撤销调课")
    @PostMapping("/change/revert")
    public Response<Void> revertClassChange(@RequestBody @Validated ClassChangeRevertReq req) {
        req.setOperateSource(OperateSourceEnum.PARENT);
        classChangeService.revertClassChange(req);
        return Response.success();
    }

    @Operation(summary = "分页查询转班调课")
    @GetMapping("/change/page")
    public Response<Pagination<StudentClassChangeRecordResp>> getChangeRecordPage(@Validated StudentClassChangeRecordQuery query, @Validated PageQuery pageQuery) {
        this.checkStudentRelation(query.getStudentId());
        Page<StudentClassChangeRecordResp> page = studentClassService.getChangeRecordPage(query, pageQuery);
        return Response.success(Pagination.of(page));
    }
}
